<?php

namespace App\Http\Controllers;

use App\Entities\Talent;
use App\Repositories\TalentRepository;
use Illuminate\Http\Request;

class TalentController  extends Controller
{
    private $talent;

    public function __construct(TalentRepository $talent)
    {
        $this->talent = $talent;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $condition = [];
//        if($request->get('class_name')){
//            $condition['name'] = ['like','%'.$request->get('class_name').'%'];
//        }
//        if($request->get('term_id')){
//            $condition['term_id'] = $request->get('term_id');
//        }


        $list = $this->talent->paginateWithCondition(10, [], $condition);
        return response()->json([
            'errors' => false,
            'count' => $list->count(),
            'data' => $list
        ]);
    }



    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $talent = $this->talent->create($request->all());
        return response()->json([
            'errors' => false,
            'message' => '新建成功',
            'data' => $talent
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Entities\Talent  $talent
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $talent = $this->talent->findByWhere('main_number',$id, ['relationships']);
        return response()->json([
            'errors' => false,
            'data' => $talent
        ]);
    }



    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Entities\Talent  $talent
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $talent = $this->talent->find($id);
        $newTerm = $this->talent->update($talent, $request->all());
        return response()->json([
            'errors'=> false,
            'data' => $newTerm
        ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Entities\Talent  $talent
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $talent = $this->talent->find($id);
        $ret = $this->talent->destroy($talent);
        return response()->json([
            'errors' =>  false,
            'data' => $ret
        ]);

    }
}
